package com.jiepos.mpos.biz.operationlog.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jiepos.mpos.common.constant.DBConstant.Table;
import com.jiepos.mpos.common.dao.LogClientKeyDao;
import com.jiepos.mpos.common.entity.App_Log_Client_KeyEntity;
import com.jiepos.mpos.core.entity.Pagination;
import com.jiepos.mpos.core.service.BaseService;
import com.jiepos.mpos.core.util.DateUtil;
import com.jiepos.mpos.core.util.StringUtils;

@Service
public class LogOnLogService extends BaseService {
	
	@Autowired
	private LogClientKeyDao logClientKeyDao;

	/**
	 * 获取客户端登陆日志Json
	 * @param pagination
	 * @param request
	 * @param app_Log_Client_KeyEntity
	 * @param appType
	 * @return
	 */
	public Map getLogOnListJson(Pagination pagination,HttpServletRequest request,App_Log_Client_KeyEntity app_Log_Client_KeyEntity,String appType,String custom_phone,String startDate,String endDate){
		
		StringBuffer sb = super.getInitSql("t1");
		List list = new ArrayList<>();
		sb.append(" ,TO_CHAR(t1.REC_CRT_TS, 'yyyy-mm-dd hh24:mi:ss') as logon_ts,t1.custom_no,nvl2(t2.login_id,decode(t2.login_id,null,'',substr(t2.login_id,1,3) || '****' || substr(t2.login_id,length(t2.login_id)-3,4)) || decode(t2.name,null,'','-'|| t2.name),'') AS custom_name,t1.client_version,")
			.append(" (select code_val|| '-' ||code_desc from T_APP_CODE_LIST where code_val = DECODE(t1.APPTYPE,'1','ANDROID','IOS') and code_tp = 'APP_TYPE') appType,")
			.append(" t1.app_id,t1.system_name,t1.system_uuid,t1.system_version,t1.system_device_model,country,province,")
			.append(" locality,sublocality,thoroughfare,subthoroughfare,localaddress,longitude,latitude FROM ")
			.append(Table.T_APP_LOG_CLIENT_KEY).append(" t1 LEFT JOIN ").append(Table.T_APP_CUSTOM_INF).append(" t2 ON T1.CUSTOM_NO = T2.CUSTOM_NO where 1=1 ");
		sb.append(" and to_char(t1.rec_crt_ts,'yyyy-MM-dd') >= ?");
		if(!StringUtils.isEmpty(startDate)){
			list.add(startDate);
		}else{
			list.add(DateUtil.getCurrDate());
		}
		sb.append(" and to_char(t1.rec_crt_ts,'yyyy-MM-dd') <= ? ");
		if(!StringUtils.isEmpty(endDate)){
			list.add(endDate);
		}else{
			list.add(DateUtil.getCurrDate());
		}
		if(!StringUtils.isEmpty(app_Log_Client_KeyEntity.getCustom_no())){
			sb.append(" and t1.custom_no like ? ");
			list.add("%"+app_Log_Client_KeyEntity.getCustom_no()+"%");
		}
		if(!StringUtils.isEmpty(custom_phone)){
			sb.append(" and t2.phone like ? ");
			list.add("%"+custom_phone+"%");
		}
		if(!StringUtils.isEmpty(appType)){
			if("ANDROID".equals(appType)){
				sb.append(" and t1.appType = ? ");
				list.add("1");
			}else if("IOS".equals(appType)){
				sb.append(" and t1.appType = ? ");
				list.add("2");
			}
		}
		if(!StringUtils.isEmpty(app_Log_Client_KeyEntity.getApp_id())){
			sb.append(" and t1.app_id like ? ");
			list.add("%"+app_Log_Client_KeyEntity.getApp_id()+"%");
		}
		//客户端android或ios系统的终端标识码
		if(!StringUtils.isEmpty(app_Log_Client_KeyEntity.getSystem_uuid())){
			sb.append(" and t1.system_uuid like ? ");
			list.add("%"+app_Log_Client_KeyEntity.getSystem_uuid()+"%");
		}
		pagination.queryString = sb.toString();
		pagination = this.getOrderSql(pagination, "t1");
		return logClientKeyDao.getPaginationMap(pagination, "t1", list);
	}
}
